package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.Logistics;


public class LogisticsAdapter extends BaseAdapter {

	Context mContext;
	List<Logistics.Logistic> mDatas;

	public LogisticsAdapter(Context mContext) {
		this.mContext = mContext;
	}

	public void update(List<Logistics.Logistic> mDatas){
		this.mDatas = mDatas;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mDatas == null ? 0 : mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_logistics, parent, false);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
			//对于listview，注意添加这一行，即可在item上使用高度
			AutoUtils.autoSize(convertView);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(position==0){
			holder.topview.setVisibility(View.INVISIBLE);
			holder.date.setTextColor(mContext.getResources().getColor(R.color.text_color_orange));
			holder.time.setTextColor(mContext.getResources().getColor(R.color.text_color_orange));
			holder.address_and_stauts.setTextColor(mContext.getResources().getColor(R.color.text_color_orange));
			holder.ciecle_view.setBackgroundResource(R.drawable.circles_shape);
		}else if(position == mDatas.size()-1){
			holder.bottom_view.setVisibility(View.INVISIBLE);
		}

		String createTime = mDatas.get(position).getCreateTimeStr();
		String [] date = createTime.split(" ");
		holder.date.setText(date[0]);
		holder.time.setText(date[1]);
		holder.address_and_stauts.setText(mDatas.get(position).getMsg());

		return convertView;
	}

	public static class ViewHolder {
		@BindView(R.id.top_view)
		View topview;
		@BindView(R.id.bottom_view)
		View bottom_view;
		@BindView(R.id.ciecle_view)
		View ciecle_view;
		@BindView(R.id.date)
		TextView date;
		@BindView(R.id.time)
		TextView time;
		@BindView(R.id.address_and_stauts)
		TextView address_and_stauts;

		ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}
}
